package com.easyfun.healthmagicbox.comm;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.HConfigurationJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.TopHealthJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.HConfiguration;
import com.easyfun.healthmagicbox.pojo.TopHealth;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.easyfun.healthmagicbox.util.SharedPreferencesUtil;
import com.j256.ormlite.dao.Dao;

public class ConfigurationDownloadTask extends BaseCommTask{

	String username; 
	String personID;
	String ipAddress;
	
	public ConfigurationDownloadTask(
			Context context,
			CommCallback callback,
			String username, 
			String personID,
			String ipAddress) 
	{

		super(context,callback);
		this.username = username;
		this.personID = personID;
		this.ipAddress = ipAddress;
	}
	
	
	@Override
	public void run() {
		JSONObject serializedObj = new JSONObject();
		String result = "";
		try {
			serializedObj.put("username", username);
			serializedObj.put("personID", personID);
			serializedObj.put("ipAddress", ipAddress);		

			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", serializedObj.toString());
			result = super.uploadByHTTP(parmas, ConstantData.ConfigurationDownloadURI);
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.FAILED) {

				ccb.setCommResult(this, result,
						ServerResponseCode.FAILED);
				return;
			}

			if ((result.length() > 0)) {
				if (ServerResponseCode.parseOf(result) == ServerResponseCode.SUCCESS) {
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				} else {
					
					HConfiguration data =  
							(HConfiguration) HConfigurationJsonWrapper
							.getInstance(result,HConfigurationJsonWrapper.class).getDataPojo();
		
					SharedPreferencesUtil.getInstance(mcontext).setACCESS_AUTH_TYPE_NAME(
							data.getAccess_AUTH_TYPE_name());
					SharedPreferencesUtil.getInstance(mcontext).setACCESS_KEY_NAME(
							data.getAccess_KEY_name());
					SharedPreferencesUtil.getInstance(mcontext).setACCESS_SECRET_NAME(
							data.getAccess_SECRET_name());
					SharedPreferencesUtil.getInstance(mcontext).setACCESS_UID_NAME(
							data.getAccess_UID_name());
					SharedPreferencesUtil.getInstance(mcontext).setAPP_KEY(
							data.getApp_key());
					SharedPreferencesUtil.getInstance(mcontext).setAPP_SECRET(
							data.getApp_secret());
					SharedPreferencesUtil.getInstance(mcontext).setSyncInterval(
							data.getSyncInterval());
					
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ccb.setCommResult(this, result,
					ServerResponseCode.FAILED);
		}
	}

}
